<?php
namespace Admin\Controller;

class SinglePageCategoryController extends BaseController
{
    public function indexAction()
    {
        //分类树
        vendor('Phpcms.Tree', '', '.class.php');
        $Tree = new \Tree();
        $Tree->icon = array('&nbsp;&nbsp;&nbsp;│ ','&nbsp;&nbsp;&nbsp;├─ ','&nbsp;&nbsp;&nbsp;└─ ');
        $Tree->nbsp = '&nbsp;&nbsp;&nbsp;';

        $SinglePageCategory = M('SinglePageCategory');
        $category_list = $SinglePageCategory->order('list_order asc, id asc')->select();
        foreach ($category_list as $key => $val) {
            $category_list[$key]['mobile_url'] = $val['type'] == 1 ? C('SITE_URL') . '/index.php?m=MobileHome&c=About&a=index&id=' . $val['id'] : "";
            $category_list[$key]['type_alias'] = $val['type'] == 1 ? "<span class='cBlue'>是</span>" : "";
            $category_list[$key]['is_on'] = $val['is_on'] == 0 ? "<span class='cBlue'>是</span>" : "";
            $category_list[$key]['str_manage'] = '<a href="' . U("SinglePageCategory/add", array('parent_id' => $val['id'])) . '">添加子栏目</a> | <a href="' . U("SinglePageCategory/update", array('id' => $val['id'])) . '">修改</a> | <a href="' . U('SinglePageCategory/delete', array('id' => $val['id'])) . '">删除</a>';
        }
        $str  = "<tr>
					<td align='center'><input name='list_order[\$item[id]]' type='text' size='3' value='\$item[list_order]' class='input-text-c input-text'></td>
					<td align='center'>\$item[id]</td>
					<td>\$spacer\$item[name]</td>
					<td><input type='text' style='width:250px;' value='\$item[mobile_url]' /></td>
					<td><img width='50' height='50' src='\$item[pic]' /></td>
					<td>\$item[type_alias]</td>
					<td>\$item[is_on]</td>
					<td>\$item[str_manage]</td>
				</tr>";
        $Tree->init($category_list);
        $category_list = $Tree->get_tree(0, $str);
        $this->assign('category_list', $category_list);
        $this->display();
    }

    public function addAction()
    {
        if (IS_POST) {
            $SinglePageCategory = D('SinglePageCategory');
            $data = $_POST['SinglePageCategory'];
            if ($SinglePageCategory->create($data)) {
                if (false !== $SinglePageCategory->add()) {
                    $this->success('添加成功！');
                } else {
                    $this->error('添加失败!');
                }
            } else {
                $this->error($SinglePageCategory->getError());
            }
        } else {
            $parent_id = isset($_GET['parent_id']) ? intval($_GET['parent_id']) : 0;

            //分类树
            vendor('Phpcms.Tree', '', '.class.php');
            $Tree = new \Tree();

            $SinglePageCategory = M('SinglePageCategory');
            $category_list = $SinglePageCategory->order('list_order asc, id asc')->select();
            foreach ($category_list as $key => $val) {
                $category_list[$key]['selected'] = $val['id'] == $parent_id ? 'selected' : '';
            }

            $str  = "<option value='\$item[id]' \$item[selected]>\$spacer \$item[name]</option>";
            $Tree->init($category_list);
            $category_list = $Tree->get_tree(0, $str);
            $this->assign('category_list', $category_list);
            $this->display();
        }
    }

    public function updateAction()
    {
        if (IS_POST) {
            $SinglePageCategory = D('SinglePageCategory');
            $data = $_POST['SinglePageCategory'];
            if ($SinglePageCategory->create($data)) {
                if (false !== $SinglePageCategory->save()) {
                    $this->success('修改成功！', U('SinglePageCategory/index'));
                } else {
                    $this->error('修改失败!');
                }
            } else {
                $this->error($SinglePageCategory->getError());
            }
        } else {
            $id = I('get.id', 0, 'intval');
            $SinglePageCategory = M('SinglePageCategory');
            $category_info = $SinglePageCategory->where('id = ' . $id)->order('list_order asc, id asc')->find();

            //分类树
            vendor('Phpcms.Tree', '', '.class.php');
            $Tree = new \Tree();
            $category_list = $SinglePageCategory->select();
            foreach ($category_list as $key => $val) {
                $category_list[$key]['selected'] = $val['id'] == $category_info['parent_id'] ? 'selected' : '';
            }
            $str  = "<option value='\$item[id]' \$item[selected]>\$spacer \$item[name]</option>";
            $Tree->init($category_list);
            $category_list = $Tree->get_tree(0, $str);

            $this->assign('category_list', $category_list);
            $this->assign('category_info', $category_info);
            $this->display();
        }
    }

    public function deleteAction()
    {
        $id = I('get.id', 0, 'intval');
        $SinglePageCategory = M('SinglePageCategory');
        $list = $SinglePageCategory->where(array('parent_id' => $id))->select();
        if (!empty($list)) {
            $this->error('该菜单下有子菜单，请先删除子菜单！');
        }

        $res = M('SinglePageCategory')->delete($id);
        if (false !== $res) {
            $this->success('删除成功！', U('SinglePageCategory/index'));
        } else {
            $this->error('删除失败!');
        }
    }

    public function listOrderAction()
    {
        $SinglePageCategory = D('SinglePageCategory');
        $arr_list_order = $_POST['list_order'];
        foreach ($arr_list_order as $key => $val) {
            $data = array('list_order' => intval($val));
            $SinglePageCategory ->where('id = ' . intval($key))->setField($data);
        }
        $this->redirect('index');
    }


    /**
     * 编辑器上传处理
     */
    public function uploadJsonAction()
    {
        $config = array(
            'savePath' => C('PICTURE_UPLOAD.rootPath')  . 'singlePageCategory/',
            'subPath' => 'singlePageCategory/',
        );
        vendor('Kindeditor.UploadJson', '', '.class.php');
        $UploadJson = new \UploadJson($config);
        $UploadJson->upload();
    }

    /**
     * 编辑器上传文件管理
     */
    public function fileManagerJson()
    {

    }
}